package com.wmyg.antifake;

import com.wmyg.antifake.sdk.ManageBuilder;
import com.wmyg.antifake.sdk.SmartManager;
import com.wmyg.antifake.utils.WebSocketServer;
import org.apache.catalina.connector.Connector;
import org.apache.coyote.AbstractProtocol;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@SpringBootApplication
@EnableAsync
public class AntifakeApplication {

    public static void main(String[] args) {
        //SpringApplication.run(AntifakeApplication.class, args);
        SpringApplication springApplication = new SpringApplication(AntifakeApplication.class);
        ConfigurableApplicationContext configurableApplicationContext = springApplication.run(args);
        WebSocketServer.setApplicationContext(configurableApplicationContext);//解决WebSocket不能注入的问题
    }

    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addConnectorCustomizers((connector) -> ((AbstractProtocol) connector.getProtocolHandler()).setKeepAliveTimeout(1));
        return tomcat;
    }

    @Bean
    public ServerEndpointExporter serverEndpointExporter (){
        return new ServerEndpointExporter();
    }

    @Bean
    public SmartManager buildSmartManager() throws Exception {
        return ManageBuilder.buildManagerToUse("south", "User1", "antifake", "mychannel");
    }

}
